package com.atguigu.ssm.controller;

import com.atguigu.ssm.pojo.Dept;
import com.atguigu.ssm.service.DeptService;
import com.atguigu.ssm.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Chunsheng Zhang 尚硅谷
 * @create 2021/10/25 14:52
 */
@Controller
public class DeptController {

    private static final String SUCCESS = "success";

    @Autowired
    private DeptService deptService;

    /**
     * 查询所有部门信息
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getAllDepts", method = RequestMethod.GET)
    public Map getAllDepts() {

        List<Dept> depts = deptService.getAllDepts();
        HashMap<String, Object> map = new HashMap<>();
        map.put("depts", depts);

        return map;
    }

    @GetMapping("/delete")
    @ResponseBody
    public Result delete(@RequestParam("deptId") Integer deptId) {
        deptService.delete(deptId);

        return new Result(true, "删除成功");
    }

    @GetMapping("/findByDeptId")
    @ResponseBody
    public Result findByDeptId(@RequestParam("deptId") Integer deptId) {

        Dept dept = deptService.getDeptById(deptId);

        return new Result(true, "查询成功", dept);
    }


    @PostMapping("/add")
    @ResponseBody
    public Result add(@RequestBody Dept dept) {
        deptService.addDept(dept);
        return new Result(true, "添加成功");

    }

    @PostMapping("/handleUpdate")
    @ResponseBody
    public Result handleUpdate(@RequestBody Dept dept) {
        deptService.handleUpdate(dept);
        return new Result(true, "更新成功");
    }

    /**
     * 简单测试添加部门
     *
     * @return
     */
    @RequestMapping("/addDept")
    public String addDept() {
        Dept dept = new Dept(null, "秘书9部门", "809");
        deptService.addDept(dept);
        return SUCCESS;
    }

}
